﻿Option Explicit On

Imports System.Text



Public Structure BranchRecord
    Public BranchID As Long
    Public Branch_Name As String
    Public Branch_Address1 As String
    Public Branch_Address2 As String
    Public Branch_Address3 As String
    Public Branch_Address4 As String
    Public Branch_Address5 As String
    Public Branch_Postcode As String
    Public Branch_CountryID As Integer
    Public Branch_TelNo As String
    Public Branch_Fax As String
    Public Branch_Website As String
End Structure


Public Class Branch

    Public Shared Function GetBranchInvoiceDetails(ByVal lngCaseID As Long) As String
        Dim dataAccess As New Global_Visas_Data_Layer.DataAccess
        Dim strSQL As String

        Try

            strSQL = "SELECt dbo.L_Branch.Branch_InvoiceFooter " & _
                     "FROM dbo.D_Case INNER JOIN " & _
                     "dbo.L_Products ON dbo.D_Case.Case_ProductID = dbo.L_Products.Product_ID INNER JOIN " & _
                     "dbo.L_ProductTypes ON dbo.L_Products.Product_Type = dbo.L_ProductTypes.ProductType_ID INNER JOIN " & _
                     "dbo.L_Branch ON dbo.L_ProductTypes.ProductType_BranchID = dbo.L_Branch.Branch_ID " & _
                     "WHERE(dbo.D_Case.Case_ID = " & CStr(lngCaseID) & ") "

            Return dataAccess.GetSQLScalar(strSQL)

        Catch ex As Exception
            'cmn.ErrorHandler(ex, "Branch.GetBranchInvoiceDetails")
            Return String.Empty
        Finally
            dataAccess = Nothing
        End Try

    End Function

    Public Shared Function FormatBranch(ByVal theBranchObject As Object) As String
        Dim dataAccess As New Global_Visas_Data_Layer.DataAccess
        Try


            Dim strAdd As String = String.Empty
            Dim theBranch As BranchRecord


            If theBranchObject Is Nothing Then
                Return String.Empty
                'Exit Function
            Else
                theBranch = CType(theBranchObject, BranchRecord)
                With theBranch
                    If .Branch_Address1 <> "" Then
                        strAdd = vbCrLf & strAdd & .Branch_Address1 & ", "
                    End If

                    If .Branch_Address2 <> "" Then
                        strAdd &= vbCrLf & .Branch_Address2 & ", "
                    End If

                    If .Branch_Address3 <> "" Then
                        strAdd &= vbCrLf & .Branch_Address3 & ", "
                    End If

                    If .Branch_Address4 <> "" Then
                        strAdd &= vbCrLf & .Branch_Address4 & ", "
                    End If

                    If .Branch_Address5 <> "" Then
                        strAdd &= vbCrLf & .Branch_Address5 & ", "
                    End If

                    If .Branch_Postcode <> "" Then
                        strAdd &= vbCrLf & .Branch_Postcode & ", "
                    End If

                    'If .Branch_CountryID <> -1 Then
                    'strAdd &= GetCountryName(.Branch_CountryID) & ", "
                    'End If

                    If Right(strAdd, 2) = ", " Then
                        strAdd = Left(strAdd, Len(strAdd) - 2)
                    End If


                End With
            End If

            Return strAdd
        Catch ex As Exception
            Return String.Empty
        Finally
            dataAccess = Nothing
        End Try
    End Function

    Public Shared Function GetCountryName(ByVal intCountry As Integer) As String
        Dim dataAccess As New Global_Visas_Data_Layer.DataAccess
        Try
            Dim strSQL As String

            strSQL = "Select cOUNTRY_Name from L_Country where Country_ID = " & CStr(intCountry)

            Return dataAccess.GetSQLScalar(strSQL)
        Catch ex As Exception
            Return String.Empty
        Finally
            dataAccess = Nothing
        End Try

    End Function

    Public Shared Function GetBranch(ByVal intBranchID) As BranchRecord
        Dim dataAccess As New Global_Visas_Data_Layer.DataAccess
        Dim cmn As New CommonFuncs

        Dim objDT As DataTable
        Dim objDR As DataRow
        Dim Record As New BranchRecord

        objDT = dataAccess.GetSQLDataTable("Select * from L_Branch where Branch_ID = " & CStr(intBranchID))

        Try
            If Not IsNothing(objDT) Then
                If objDT.Rows.Count > 0 Then
                    objDR = objDT.Rows(0)
                    With Record
                        .BranchID = cmn.GetValue(objDR, "Branch_ID", -1)
                        .Branch_Address1 = cmn.GetValue(objDR, "Branch_Address1", "")
                        .Branch_Address2 = cmn.GetValue(objDR, "Branch_Address2", "")
                        .Branch_Address3 = cmn.GetValue(objDR, "Branch_Address3", "")
                        .Branch_Address4 = cmn.GetValue(objDR, "Branch_Address4", "")
                        .Branch_Address5 = cmn.GetValue(objDR, "Branch_Address5", "")
                        .Branch_CountryID = cmn.GetValue(objDR, "Branch_CountryID", -1)
                        .Branch_Fax = cmn.GetValue(objDR, "Branch_Fax", "")
                        .Branch_Name = cmn.GetValue(objDR, "Branch_Name", "")
                        .Branch_Postcode = cmn.GetValue(objDR, "Branch_Postcode", "")
                        .Branch_TelNo = cmn.GetValue(objDR, "Branch_TelNo", "")
                        .Branch_Website = cmn.GetValue(objDR, "Branch_Website", "")
                    End With

                End If
            End If

            Return Record

        Catch ex As Exception
            Return Nothing
            GetBranch = Nothing
        Finally
            objDT = Nothing
            dataAccess = Nothing
        End Try

    End Function

    Public Shared Function UpdateBranch(ByVal Record As BranchRecord) As Boolean
        Dim dataAccess As New Global_Visas_Data_Layer.DataAccess
        Dim SQL As New StringBuilder
        Try
            SQL = New StringBuilder

            With SQL
                .Append("update l_Branch set ")
                .Append("Branch_Name = '" & Record.Branch_Name & "', ")
                .Append("Branch_Address1 = '" & Record.Branch_Address1 & "', ")
                .Append("Branch_Address2 = '" & Record.Branch_Address2 & "', ")
                .Append("Branch_Address3 = '" & Record.Branch_Address3 & "', ")
                .Append("Branch_Address4 = '" & Record.Branch_Address4 & "', ")
                .Append("Branch_Address5 = '" & Record.Branch_Address5 & "', ")
                .Append("Branch_Postcode = '" & Record.Branch_Postcode & "', ")
                .Append("Branch_CountryID = " & Record.Branch_CountryID & ", ")
                .Append("Branch_TelNo='" & Record.Branch_TelNo & "', ")
                .Append("Branch_Website='" & Record.Branch_Website & "', ")
                .Append("Branch_Fax='" & Record.Branch_Fax & "' ")
                .Append("where branch_id = " & Record.BranchID)

            End With

            Return dataAccess.ExecuteSQL(SQL.ToString)
        Catch ex As Exception
            Return False
        Finally
            dataAccess = Nothing
            Sql = Nothing
        End Try
    End Function

    Public Shared Function GetAllBranches() As BranchRecord()
        Dim dataAccess As New Global_Visas_Data_Layer.DataAccess
        Dim cmn As New CommonFuncs
        Dim objDT As DataTable
        Dim objDR As DataRow = Nothing
        Dim Record() As BranchRecord
        Dim intCtr As Integer
        Dim intRecords As Integer


        Try
            objDT = dataAccess.GetSQLDataTable("Select * from L_Branch")

            If Not IsNothing(objDT) Then
                If objDT.Rows.Count > 0 Then
                    intRecords = objDT.Rows.Count
                    ReDim Record(0 To intRecords - 1)
                    intRecords = 0
                    For intCtr = 0 To intRecords
                        objDR = objDT.Rows(intCtr)
                        With Record(intCtr)
                            .BranchID = cmn.GetValue(objDR, "Branch_ID", -1)
                            .Branch_Address1 = cmn.GetValue(objDR, "Branch_Address1", "")
                            .Branch_Address2 = cmn.GetValue(objDR, "Branch_Address2", "")
                            .Branch_Address3 = cmn.GetValue(objDR, "Branch_Address3", "")
                            .Branch_Address4 = cmn.GetValue(objDR, "Branch_Address4", "")
                            .Branch_Address5 = cmn.GetValue(objDR, "Branch_Address5", "")
                            .Branch_CountryID = cmn.GetValue(objDR, "Branch_CountryID", -1)
                            .Branch_Fax = cmn.GetValue(objDR, "Branch_Fax", "")
                            .Branch_Name = cmn.GetValue(objDR, "Branch_Name", "")
                            .Branch_Postcode = cmn.GetValue(objDR, "Branch_Postcode", "")
                            .Branch_TelNo = cmn.GetValue(objDR, "Branch_TelNo", "")
                            .Branch_Website = cmn.GetValue(objDR, "Branch_Website", "")
                        End With
                    Next
                    Return Record
                Else
                    Return Nothing
                End If
            Else
                Return Nothing
            End If

        Catch ex As Exception
            'cmn.ErrorHandler(ex, "Branch.GetBranch", True)
            GetAllBranches = Nothing
            Return Nothing
        Finally
            objDT = Nothing
        End Try
    End Function


End Class
